﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#if 0
int main()
{
	//int a = -5;
	// 10000101 原码
	// 11111010 反码
	// 11111011 补码

	// >>
	// <<
	//移位操作符，移动的是二进制数
	//操作数只能是整数

	int a = 6;
	int b = (a << 1);

	printf("%d\n", b);
	printf("%d\n", a);

	int c = -6;
	int d = (c << 3);
	printf("%d\n", c);
	printf("%d\n", d);

	return 0;
}
#endif

#if 0
int main()
{
	// >>右移
	// 左边⽤原该值的符号位填充，右边丢弃
	int a = -1;
	int b = (a >> 1);
	//大部分情况下有除2的效果
	printf("%d\n", a);
	printf("%d\n", b);

	return 0;
}
#endif

 //按位与   & 全才1为1，全0才为0
 //按位或   | 有1为1 全0为0
 //按位异或 ^ 相同为0，相异为1
 //按位取反 ~ 每一位都取反

#if 0
int mian()
{
	int a = 3;
	int b = 5;
	printf("交换前：\na = %d\nb = %d\n", a, b);
	a = b;
	printf("交换后：\na = %d\n", a);
	b = a;
	printf("交换后：\na = %d\n", b);

	return 0;
}
#endif

//针对负数有问题
#if 0
int main()
{
	int n = 0;
	scanf("%d", &n);
	int count = 0;
	while (n)
	{
		if (n % 2 == 1)
			count++;
		n /= 2;
	}
	printf("%d\n", count);
	return 0;
}
#endif

#if 0
int main()
{
	unsigned int n = 0;
	scanf("%d", &n);
	int count = 0;
	while (n)
	{
		if (n % 2 == 1)
			count++;
		n /= 2;
	}
	printf("%d\n", count);
	return 0;
}
#endif

#if 0
int main()
{
	int n = 0;
	scanf("%d", &n);
	int count = 0;
	for (int i = 0; i < 32; i++)
	{
		if (((n >> i) & 1 )== 1)
			count++;
	}
	printf("%d\n", count);
	return 0;
}
#endif


#if 0
int main()
{
	int n = 0;
	scanf("%d", &n);
	int count = 0;
	while (n)
	{
		n = n & (n - 1);
		count++;
	}
	printf("%d\n", count);
	return 0;
}
#endif

#include <stdio.h>

#if 0
int isPowerOfTwo(int n) {
	if (n <= 0) {
		return 0;
	}
	while (n % 2 == 0) {
		n /= 2;
	}
	return (n == 1);
}

int main() 
{
	int num;
	printf("请输入一个整数: ");
	scanf("%d", &num);

	if (isPowerOfTwo(num)) 
	{
		printf("%d 是 2 的次方数\n", num);
	}
	else 
	{
		printf("%d 不是 2 的次方数\n", num);
	}

	return 0;
}
#endif

#if 0
//编写代码将13⼆进制序列的第5位修改为1，然后再改回0
//置0或置1
int main()
{
	int a = 13;
	a = a | (1 << 4);
	printf("a = %d\n", a);
	a = a & ~(1 << 4);
	printf("a = %d\n", a);
	return 0;
}
#endif

#if 0
int main()
{
	int n = 0;
	scanf("%d", &n);
	int count = 0;
	while (n)
	{
		n = n & (n - 1);
		count++;
	}
	if (count == 1)
	{
		printf("是2的次方数\n");
	}
	else
	{
		printf("不是\n");
	}
	printf("%d\n", count);
	return 0;
}
#endif

#if 0
int main()
{
	int n = 0;
	scanf("%d", &n);
	if (n & (n - 1) == 0)
		printf("%d是2的次方数\n", n);
	else
		printf("%d不是2的次方数\n", n);
	return 0;
}
#endif

#if 0
int main()
{
	int a = 1;
	int b = 2;
	int c = (a > b, a = b + 10, a, b = a + 1);
	//逗号表达式，从左向右依次执⾏。
	// 整个表达式的结果是最后⼀个表达式的结果。
	printf("%d", c);
	return;
}
#endif

#if 0
int main()
{
	int x = 9;
	//1001
	int y = 3;
	//0011
	int z = x ^ y;
	//1010 - 直接是补码转换成10进制数
	printf("%d\n", z);
	return 0;
}
#endif 

#if 0
int main()
{
	int a = 12;
	//1100
	int b = 5;
	//0101
	int c = a & b;
	//0100
	printf("%d\n", c);

	return 0;
}
#endif

#if 0
int main()
{
	int a = 3;
	int b = 5;
	printf("交换前:a = %d\n", a);
	printf("交换前:b = %d\n", b);
	a = a ^ b;
	b = b ^ a;
	a = a ^ b;
	printf("交换后:a = %d\n", a);
	printf("交换后:b = %d\n", b);
	return 0;
}
#endif

#if 0
int A(int n)
{
	int count = 0;
	while (n)
	{
		n = n & (n - 1);
		count++;
	}
	return count;
}

int main()
{
	int n = 0;
	scanf("%d", &n);
	int ret = A(n);
	printf("%d个1\n", ret);
	return 0;
}
#endif

#if 0
int main()
{
	int n = 0;
	int count = 0;
	scanf("%d", &n);
	while (n)
	{
		n = n & (n - 1);
		count++;
	}
	printf("%d\n", count);
}
#endif

#if 1
//找二进制位的有多少位不同
int funcBit(int n, int m)
{
	int tmp = m ^ n;//异或找不同，用tmp变量接收
	int count = 0;
	while (tmp != 0)
	{
		//然后利用临时创建的变量来	
		//计算tmp有多少个1就可
		tmp = tmp & (tmp - 1);					
		count++;
	}
	return count;
}

int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);// 12 5
	int ret = funcBit(a, b);
	printf("有%d个不同的1\n", ret);// 2
	return 0;
}
#endif

